import cheerio from 'cheerio';
import { log } from 'console';

export function countItems(arr:any) {  
  let count = 0;  
  for (let item of arr) {  
      // 当前对象本身是一个item，所以count加1  
      count++;  
      // 如果item有Children属性且是一个数组，则递归计算Children中的item数  
      if (item.hasOwnProperty('Children') && Array.isArray(item.Children)) {  
          count += countItems(item.Children);  
      }  
  }  
  return count;  
}  



//防抖函数
export function debounce(func: Function, delay: number) {
  
  let timeoutId: ReturnType<typeof setTimeout>;

  return function(this: any, ...args: any[]) {
    const context = this;

    clearTimeout(timeoutId);

    timeoutId = setTimeout(() => {
      func.apply(context, args);
    }, delay);
  };
}



//节流函数
export function throttle(func: Function, delay: number) {
  let timeoutId: ReturnType<typeof setTimeout> | undefined;
  let lastExecTime = 0;

  return function(this: any, ...args: any[]) {
    const currentTime = Date.now();
    const remainingTime = delay - (currentTime - lastExecTime);

    if (timeoutId) {
      clearTimeout(timeoutId);
    }

    if (remainingTime <= 0) {
      func.apply(this, args);
      lastExecTime = currentTime;
    } else {
      timeoutId = setTimeout(() => {
        func.apply(this, args);
        lastExecTime = Date.now();
      }, remainingTime);
    }
  };
}


//返回时间字符串函数
export function formatRelativeTime(inputTimeString: string): string {
  const inputTime = new Date(inputTimeString);
  const currentTime = new Date();
  const timeDifference = currentTime.getTime() - inputTime.getTime();
  const secondsDifference = Math.floor(timeDifference / 1000);
  const minutesDifference = Math.floor(secondsDifference / 60);
  const hoursDifference = Math.floor(minutesDifference / 60);
  const daysDifference = Math.floor(hoursDifference / 24);
  const monthsDifference = Math.floor(daysDifference / 30);
  const yearsDifference = Math.floor(monthsDifference / 12);

  if (yearsDifference > 0) {
      return yearsDifference === 1 ? '1 年前' : `${yearsDifference} 年前`;
  } else if (monthsDifference > 0) {
      return monthsDifference === 1 ? '1 个月前' : `${monthsDifference} 个月前`;
  } else if (daysDifference > 0) {
      return daysDifference === 1 ? '1 天前' : `${daysDifference} 天前`;
  } else if (hoursDifference > 0) {
      return hoursDifference === 1 ? '1 小时前' : `${hoursDifference} 小时前`;
  } else if (minutesDifference > 0) {
      return minutesDifference === 1 ? '1 分钟前' : `${minutesDifference} 分钟前`;
  } else {
      return '刚刚';
  }
}

export function extractTextFromHTML(html: string): string {
  // 使用 cheerio 加载 HTML 字符串
  const $ = cheerio.load(html);
  // 提取纯文本内容
  const text = $('body').text();
  // 截取前 200 个字符
  const truncatedText = text.substring(0, 200);
  return truncatedText;
}


export function parseDate(dateString:string) {
  const months = [
      "January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"
  ];
  
  // 拆分日期字符串
  const [year, month, day] = dateString.split("-");

  // 返回包含年、月（英文）和日的对象
  return {
      year: year,
      month: months[parseInt(month) - 1],
      day: day
  };
}


export function formatNumber(num:any) {  
  if(num===null){
    return ""
  }
  if (num < 1000) {  
      // 小于1000，直接返回原数字  
      return num.toString();  
  } else if (num >= 1000 && num < 10000) {  
      // 1000到9999之间，返回1k到9k  
      if (num % 1000 === 0) {  
        // 能整除，返回w形式  
        return Math.floor(num / 1000) + 'k'; 
    } else {  
        // 不能整除，返回w+形式  
        return Math.floor(num / 1000) + 'k+'; 
    } 
       
  } else if (num >= 10000) {    
      // 10000及以上，先判断是否能整除10000  
      if (num % 10000 === 0) {  
          // 能整除，返回w形式  
          return Math.floor(num / 10000) + 'w';  
      } else {  
          // 不能整除，返回w+形式  
          return Math.floor(num / 10000) + 'w+';  
      }  
  }  
}  



